﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SaturnExplorer.Views;

namespace SaturnExplorer
{
	public partial class ApplicationWindow : ChromeWindow
	{
		public ApplicationWindow()
		{
			InitializeComponent();

			CommandManager.RegisterClassCommandBinding(typeof(ApplicationWindow), new CommandBinding(CloseApplicationCommand, OnCloseApplicationCommandExecuted));
			CommandManager.RegisterClassCommandBinding(typeof(ApplicationWindow), new CommandBinding(MinimizeCommand, OnMinimizeCommandExecuted));
			CommandManager.RegisterClassCommandBinding(typeof(ApplicationWindow), new CommandBinding(MaximizeCommand, OnMaximizeCommandExecuted));
			CommandManager.RegisterClassCommandBinding(typeof(ApplicationWindow), new CommandBinding(NormalizeCommand, OnNormalizeCommandExecuted));

            Loaded += OnLoaded;
		}

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            var title = (Grid)Template.FindName("PART_Title", this);
            if (null != title)
            {
                title.MouseLeftButtonDown += OnTitleMouseLeftButtonDown;
                title.MouseLeftButtonDown += TitleMouseLeftButtonDown;
            }

            Content = new MediaElementsView();
        }

        private void TitleMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount > 1)
            {
                if (WindowState != WindowState.Maximized)
                {
                    WindowState = WindowState.Maximized;
                }
                else
                {
                    WindowState = WindowState.Normal;
                }
            }
        }

        private void OnTitleMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

		#region Commands

        /// <summary>
		/// Minimize command
		/// </summary>
		public static RoutedCommand MinimizeCommand = new RoutedCommand();

		/// <summary>
		/// Maximize command
		/// </summary>
		public static RoutedCommand MaximizeCommand = new RoutedCommand();

		/// <summary>
		/// Normalize command
		/// </summary>
		public static RoutedCommand NormalizeCommand = new RoutedCommand();

		/// <summary>
		/// Close command
		/// </summary>
		public static RoutedCommand CloseApplicationCommand = new RoutedCommand();

		#endregion

		#region Event handlers
        
		private void OnMinimizeCommandExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			WindowState = WindowState.Minimized;
		}

		private void OnNormalizeCommandExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			WindowState = WindowState.Normal;
		}

		private void OnMaximizeCommandExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			WindowState = WindowState.Maximized;
		}

		private void OnCloseApplicationCommandExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			Close();
		}

        #endregion
	}
}
